package com.example.amazinglistview;

import java.util.ArrayList;
import java.util.List;

import com.example.amazinglistview.utils.Logger;
import com.example.amazinglistview.view.AmazingAdapter;
import com.example.amazinglistview.view.AmazingListView;

import android.os.Bundle;
import android.util.Pair;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.app.Activity;
import android.graphics.Color;

public class MainActivity extends Activity {

	private ArrayList<View> headerList;
	private ArrayList<Object> list1;
	private ArrayList<Object> list2;
	private LayoutInflater mInflater;
	private View mSectionLayout;
	private ArrayList<Object> list3;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		initView();
	}

	private void initView() {
		headerList = new ArrayList<View>();
		View header1 = getLayoutInflater().inflate(R.layout.header1, null);
		View header2 = getLayoutInflater().inflate(R.layout.header2, null);
		headerList.add(header1);
		headerList.add(header2);

		list1 = new ArrayList<Object>();
		list2 = new ArrayList<Object>();
		list3 = new ArrayList<Object>();
		for (int i = 0; i < 20; i++) {
			list1.add("This is first listview item" + i);
			list3.add("This is third listview item" + i);
			list2.add("This is second listview item" + i);
		}

		mInflater = getLayoutInflater();

		AmazingListView listView = (AmazingListView) findViewById(R.id.lv_main);
		mSectionLayout = getLayoutInflater().inflate(R.layout.section_layout,
				listView, false);
		listView.setPinnedHeaderView(mSectionLayout);
		listView.setAdapter(new MyAdapter());
	}

	private class MyAdapter extends AmazingAdapter {

		private List<Pair<String, List<Object>>> data;

		public MyAdapter() {
			data = new ArrayList<Pair<String, List<Object>>>();
			Pair<String, List<Object>> pair1 = new Pair<String, List<Object>>(
					"first", list1);
			Pair<String, List<Object>> pair2 = new Pair<String, List<Object>>(
					"second", list2);
			Pair<String, List<Object>> pair3 = new Pair<String, List<Object>>(
					"third", list3);
			data.add(pair1);
			data.add(pair3);
			data.add(pair2);
		}

		@Override
		public int getCount() {
			int count = headerList.size() + list1.size() + list2.size()
					+ list3.size();
			return count;
		}

		@Override
		public Object getItem(int position) {
			Object result = null;

			if (position == 0) {
				result = headerList.get(0);
			} else if (position > 0
					&& position < list1.size() + list3.size() + 1) {
				if (position < list1.size() + 1) {
					result = list1.get(position - 1);
				} else {
					result = list3.get(position - 1 - list1.size());
				}
			} else if (position == list1.size() + list3.size() + 1) {
				result = headerList.get(1);
			} else if (position > list1.size() + list3.size() + 1
					&& position < getCount()) {
				result = list2.get(position - 2 - list1.size() - list3.size());
			}

			return result;
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		protected void onNextPageRequested(int page) {

		}

		@Override
		protected void bindSectionHeader(View view, int position,
				boolean displaySectionHeader) {
			if (displaySectionHeader) {
				if (position < list1.size() + list2.size() + 1) {
					View flSection = view
							.findViewById(R.id.section_item1);
					TextView tvSection = (TextView) flSection.findViewById(R.id.tv_section);
					tvSection.setVisibility(View.VISIBLE);
					tvSection.setText((String)getItem(position));
					Logger.i(TAG, (String) getItem(position));
				} else if (position == list1.size() + list2.size() + 1) {

				}
			}

			if (position == 0) {
			} else if (position > 0
					&& position < list1.size() + list3.size() + 1) {
				if (position == 1) {
					view.findViewById(R.id.section_item1).setVisibility(
							View.VISIBLE);
				} else if (position == list1.size() + 1) {
					view.findViewById(R.id.section_item1).setVisibility(
							View.VISIBLE);
				}
			} else if (position == list1.size() + 1) {
			} else if (position > list1.size() + 1 && position < getCount()) {
			}
		}

		@Override
		public View getAmazingView(int position, View convertView,
				ViewGroup parent) {
			Logger.e(TAG, "position = "+position);
			if (position == 0) {
				return (View) getItem(position);
			} else if (position > 0
					&& position < list1.size() + list3.size() + 1) {
				ViewHolder1 holder1 = null;
				if (convertView == null) {
					convertView = mInflater.inflate(R.layout.item1, null);
					holder1 = new ViewHolder1();
					holder1.tvSection = convertView
							.findViewById(R.id.section_item1);
					holder1.tvItem1 = (TextView) convertView
							.findViewById(R.id.tv_item1);
					convertView.setTag(holder1);
				} else {
					Object tempHolder = convertView.getTag();
					if (tempHolder instanceof ViewHolder1) {
						// Logger.e(TAG, "is holder1");
						holder1 = (ViewHolder1) tempHolder;
						holder1.tvSection.setVisibility(View.GONE);
					} else {
						// Logger.e(TAG, "is not holder1");
						convertView = mInflater.inflate(R.layout.item1, null);
						holder1 = new ViewHolder1();
						holder1.tvSection = convertView
								.findViewById(R.id.section_item1);
						holder1.tvItem1 = (TextView) convertView
								.findViewById(R.id.tv_item1);
						convertView.setTag(holder1);
					}
				}
				String str = (String) getItem(position);
				holder1.tvItem1.setText(str);
			} else if (position == list1.size() + list3.size() + 1) {
				return (View) getItem(position);
			} else if (position > list1.size() + list3.size() + 1
					&& position < getCount()) {
				ViewHolder2 holder2 = null;
				if (convertView == null) {
					convertView = mInflater.inflate(R.layout.item2, null);
					holder2 = new ViewHolder2();
					holder2.tvSection = convertView
							.findViewById(R.id.section_item2);
					holder2.btnItem2 = (Button) convertView
							.findViewById(R.id.btn_item2);
					convertView.setTag(holder2);
				} else {
					Object tempHolder = convertView.getTag();
					if (tempHolder instanceof ViewHolder2) {
						// Logger.e(TAG, "is holder2");
						holder2 = (ViewHolder2) tempHolder;
						holder2.tvSection.setVisibility(View.GONE);
					} else {
						// Logger.e(TAG, "is not holder2");
						convertView = mInflater.inflate(R.layout.item2, null);
						holder2 = new ViewHolder2();
						holder2.tvSection = convertView
								.findViewById(R.id.section_item2);
						holder2.btnItem2 = (Button) convertView
								.findViewById(R.id.btn_item2);
						convertView.setTag(holder2);
					}
				}
				String str = (String) getItem(position);
				holder2.btnItem2.setText(str);
			}

			return convertView;
		}

		@Override
		public void configurePinnedHeader(View header, int position, int alpha) {
			View sectionLayout = mSectionLayout;
			TextView tvSec1 = (TextView) sectionLayout
					.findViewById(R.id.tv_section);
			if (position < list1.size() + list3.size() + 1) {
				findViewById(R.id.second_header).setVisibility(View.GONE);
				if (position == 0) {
					tvSec1.setText("");
					tvSec1.setBackgroundColor(Color.argb(0, 0, 0, 0));
				} else {
					tvSec1.setText(getSections()[getSectionForPosition(position)]
							.toString());
					tvSec1.setBackgroundColor(Color.argb(99, 0, 0, 0));
				}
			}

			if (position == list1.size() + list3.size() + 1) {
				tvSec1.setText("");
				tvSec1.setBackgroundColor(Color.argb(0, 0, 0, 0));
				findViewById(R.id.second_header).setVisibility(View.VISIBLE);
			} else if (position > list1.size() + 1 && position < getCount()) {
			}

		}

		@Override
		public int getPositionForSection(int section) {
			if (section < 0)
				section = 0;
			if (section >= data.size())
				section = data.size() - 1;
			int c = 0;
			for (int i = 0; i < data.size(); i++) {
				if (section == i) {
					return c + 1;// TODO
				}
				c += data.get(i).second.size();
			}
			return 0;
		}

		@Override
		public int getSectionForPosition(int position) {
			int c = 0;
			for (int i = 0; i < data.size(); i++) {
				if (position - 1 >= c
						&& position - 1 < c + data.get(i).second.size()) {// TODO
					return i;
				}
				c += data.get(i).second.size();
			}
			return -1;
		}

		@Override
		public Object[] getSections() {
			return new String[] { "This is first listview item0", "This is second listview item0"};
		}

	}

	private class ViewHolder1 {
		private View tvSection;
		private TextView tvItem1;
	}

	private class ViewHolder2 {
		private View tvSection;
		private Button btnItem2;
	}
}
